package com.liuzh.kaoyanspringboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liuzh.kaoyanspringboot.common.Constants;
import com.liuzh.kaoyanspringboot.common.Result;
import com.liuzh.kaoyanspringboot.entity.Like;
import com.liuzh.kaoyanspringboot.exception.ServiceException;
import com.liuzh.kaoyanspringboot.service.ArticleService;
import com.liuzh.kaoyanspringboot.service.LikeService;
import com.liuzh.kaoyanspringboot.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 刘志鸿
 * @since 2023-03-12
 */
@RestController
@RequestMapping("/like")
public class LikeController {

    @Autowired
    private LikeService likeService;
    @Autowired
    private ArticleService articleService;

    // 新增或更新
    @PostMapping
    public Result save(@RequestBody Like like) {
        // 查看数据库中是已经点赞过
        Like like1 = findALike(like);
        if (like1 == null) {
            likeService.saveLike(like);
            return  Result.success(true);
        }
        if (like1.getIsDelete()) {  // 如果是点赞后取消的
            like1.setIsDelete(false);  // 回复点赞状态
            likeService.addLike(like1);
            return  Result.success(true);   // 返回是点赞
        }
        else {
            like1.setIsDelete(true);  // 取消点赞状态
            likeService.cancelLike(like1);
            return  Result.success(false);
        }
    }

    // 根据 id 删除
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        return Result.success(likeService.removeById(id));
    }

    // 批量删除
    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        return Result.success(likeService.removeBatchByIds(ids));
    }

    //查询所有数据
    @GetMapping
    public Result findAll() {
        return Result.success(likeService.list());
    }

    // 根据 id 查询
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(likeService.getById(id));
    }

    // 根据 id 查询
//    @GetMapping("/userLike")
//    public Result findLike(@RequestParam Integer userId,
//                           @RequestParam Integer likeId,
//                           @RequestParam String type) {
//        Like like = new Like();
//        like.setType(type);
//        like.setUserId(userId);
//        like.setLikeId(likeId);
//        if (findALike(like) == null) {
//            return Result.success(false);
//        } else return Result.success(true);
//    }
    @GetMapping("/userLike")
    @ResponseBody
    public Result findLike( Like like) {
//        Like like = new Like();
//        like.setType(type);
//        like.setUserId(userId);
//        like.setLikeId(likeId);
        if (findALike(like) == null) {
            return Result.success(false);
        } else return Result.success(true);
    }

    //简单的分页查询
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {
        QueryWrapper<Like> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        return Result.success(likeService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

    public Like findALike(Like like) {
        Integer realUserId = TokenUtils.getCurrentUser().getId();
        if (like.getUserId() == null) {
            like.setUserId(realUserId);
        }
        if (like.getLikeId() == null || like.getType() ==null || like.getUserId() != realUserId) {
            throw new ServiceException(Constants.CODE_401,"参数错误");
        }
        return likeService.selectUserIsLike(like);
    }
}

